package api;

import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/regist")
public class RegistServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        if(username==null||username.equals("")||password==null||password.equals("")){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("<h1>你未输入用户名或密码！</h1>");
            return;
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        UserDao userDao=new UserDao();
        userDao.add(user);
        resp.getWriter().write("注册成功！");
        resp.sendRedirect("login.html");
    }
}
